//
//  UIBarButtonItem+Create.m
//  BjOilMember
//
//  Created by QingSong_Hao on 15/8/17.
//  Copyright (c) 2015年 石化盈科信息技术有限责任公司. All rights reserved.
//

#import "UIBarButtonItem+Create.h"

@implementation UIBarButtonItem (Create)

+ (UIBarButtonItem *)barButtonItemWithIcon:(NSString *)icon withTarget:(id)target withSelector:(SEL)selector
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    CGSize size = CGSizeMake(30, 30);
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    btn.bounds = (CGRect){CGPointZero, size};

    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)barButtonItemWithIcon:(NSString *)icon withTarget:(id)target withSelector:(SEL)selector withSize:(CGSize)size
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    btn.bounds = (CGRect){CGPointZero, size};
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)barButtonItemImage:(NSString *)image withTarget:(id)target withSelector:(SEL)selector withSize:(CGSize)size
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    btn.bounds = (CGRect){CGPointZero, {44,44}};

    btn.backgroundColor = [UIColor clearColor];
    UIImageView *btnImage = [[UIImageView alloc]initWithFrame:CGRectMake(13+ (44-size.width)/2, (44-size.height)/2, size.width, size.height)];
    btnImage.image = [UIImage imageNamed:image];
    [btn addSubview:btnImage];
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}


+ (UIBarButtonItem *)barButtonItemImage:(NSString *)image withTarget:(id)target withSelector:(SEL)selector withSize:(CGSize)size withLabelText:(NSString *)text
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor orangeColor];
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    btn.bounds = (CGRect){CGPointZero, {44,44}};
    
    btn.backgroundColor = [UIColor clearColor];
    UIImageView *btnImage = [[UIImageView alloc]initWithFrame:CGRectMake(13+ (44-size.width)/2, (44-size.height)/2, size.width, size.height)];
    btnImage.image = [UIImage imageNamed:image];
    [btn addSubview:btnImage];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(34, 4, 20, 20)];
    label.layer.cornerRadius = 10.0f;
    label.layer.masksToBounds = YES;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:12];
    label.backgroundColor = [UIColor redColor];
    label.text = text;
    [btn addSubview:label];
    
    if([text isEqualToString:@"0"]){
        label.hidden = YES;
    }else{
        label.hidden = NO;
    }
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)barButtonItemTitle:(NSString *)title withTitleColor:(UIColor *)titleColor withFont:(int)fontSize withTarget:(id)target withSelector:(SEL)selector
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = (CGRect){CGPointZero,{100,44}};
    UILabel *label = [[UILabel alloc]initWithFrame:btn.bounds];
    label.text = title;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = titleColor;
    label.font = [UIFont systemFontOfSize:fontSize];
    label.textAlignment = NSTextAlignmentRight;
    [btn addSubview:label];
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)bar1ButtonItemTitle:(NSString *)title withTitleColor:(UIColor *)titleColor withFont:(int)fontSize withTarget:(id)target withSelector:(SEL)selector
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = (CGRect){CGPointZero,{44,44}};
    UILabel *label = [[UILabel alloc]initWithFrame:btn.bounds];
    label.text = title;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = titleColor;
    label.font = [UIFont systemFontOfSize:fontSize];
    label.textAlignment = NSTextAlignmentRight;
    [btn addSubview:label];
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

@end
